package qqserver.service;

import qqcommon.Message;
import qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

public class ServerConnectClientThread  extends Thread {
    // Socket对象，用于和服务端通信
    private Socket socket;
    // 用户ID，标识连接的用户
    private String userId;

    public ServerConnectClientThread(Socket socket, String userId) {
        this.socket = socket;
        this.userId = userId;
    }

    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {
        while (true)
        {
            try {
                System.out.println("服务端和客户端保持通信,"+userId+"读取数据...");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();
                //根据message的type，来处理不同的业务
                if(message.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND))
                {
                    System.out.println(message.getSender()+"请求在线用户列表");
                    String onlineUsers = ManageClientThreads.getOnlineUsers();
                    Message message2 = new Message();
                    message2.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                    message2.setContent(onlineUsers);
                    message2.setGetter(message.getSender());
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(message2);
                }
                else if(message.getMesType().equals(MessageType.MESSAGE_COMM_MES))
                {
                    ServerConnectClientThread serverConnectClientThread = ManageClientThreads.getServerConnectClientThread(message.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream());
                    oos.writeObject(message);
                }
                else if(message.getMesType().equals(MessageType.MESSAGE_TO_ALL))
                {
                    HashMap<String, ServerConnectClientThread> hm = ManageClientThreads.getHm();
                    Iterator<String> iterator = hm.keySet().iterator();
                    while (iterator.hasNext())
                    {
                        String userId = iterator.next();
                        if(!userId.equals(message.getSender()))
                        {
                            ObjectOutputStream oos = new ObjectOutputStream(hm.get(userId).getSocket().getOutputStream());
                            oos.writeObject(message);
                        }
                    }
                }
                else if(message.getMesType().equals(MessageType.MESSAGE_FILE_MES))
                {
                    ServerConnectClientThread serverConnectClientThread = ManageClientThreads.getServerConnectClientThread(message.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream());
                    oos.writeObject(message);

                }
                else if(message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT))
                {
                    System.out.println(message.getSender()+"退出");
                    ManageClientThreads.removeClientThread(message.getSender());
                    socket.close();
                    break;
                }
                else
                {
                    System.out.println("其他类型的message暂时不处理");
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}
